﻿Imports System.IO
Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports GeoFramework
Imports GeoFramework.Gps
Imports GeoFramework.Gps.IO
Imports GeoFramework.Gps.Nmea


Public Class frmParamGPS

    Public oCurrentDevice As Device
    Public NmeaInterpreter1 As New GeoFramework.Gps.Nmea.NmeaInterpreter()
    Private strLatitude As String = ""
    Private strLongitude As String = ""
    Private strAltitude As String = "00000"
    Private strHDOP As String = ""
    Private strGPSparam As String
    Public blGpsON As Boolean = False
    Dim GpsStatus As CtlLightGPSTrack.GpsLightStatus

    'Déclaration du délégué d'affichage de track GPS
    ' Private Delegate Sub DelegateAfficherDataTrack(ByVal wLatitude As String, ByVal wLongitude As String, _
    '  ByVal wDop As String, ByVal wAltitude As String)
    ' Private pAfficherDataTrack As New DelegateAfficherDataTrack(AddressOf AfficherDataTrack)

    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Show()
        Application.DoEvents()
        m_isFormGPSOpen = True

        AddHandler Devices.DeviceDetectionStarted, AddressOf Devices_DeviceDetectionStarted
        AddHandler Devices.DeviceDetected, AddressOf Devices_DeviceDetected
        AddHandler Devices.DeviceDetectionCompleted, AddressOf Devices_DeviceDetectionCompleted
        AddHandler Devices.DeviceDetectionAttempted, AddressOf Devices_DeviceDetectionAttempted
        AddHandler Devices.DeviceDetectionAttemptFailed, AddressOf Devices_DeviceDetectionAttemptFailed

        AddHandler NmeaInterpreter1.SpeedChanged, AddressOf NmeaInterpreter1_SpeedChanged
        AddHandler NmeaInterpreter1.FixAcquired, AddressOf NmeaInterpreter1_FixAcquired
        AddHandler NmeaInterpreter1.FixLost, AddressOf NmeaInterpreter1_FixLost
        AddHandler NmeaInterpreter1.Resumed, AddressOf NmeaInterpreter1_Resumed
        AddHandler NmeaInterpreter1.FixModeChanged, AddressOf NmeaInterpreter1_FixModeChanged
        AddHandler NmeaInterpreter1.FixQualityChanged, AddressOf NmeaInterpreter1_FixQualityChanged
        AddHandler NmeaInterpreter1.HorizontalDilutionOfPrecisionChanged, AddressOf NmeaInterpreter1_HorizontalDilutionOfPrecisionChanged
        AddHandler NmeaInterpreter1.SentenceReceived, AddressOf NmeaInterpreter1_SentenceReceived
        AddHandler NmeaInterpreter1.AltitudeChanged, AddressOf NmeaInterpreter1_AltitudeChanged
        AddHandler NmeaInterpreter1.PositionChanged, AddressOf NmeaInterpreter1_PositionChanged
        AddHandler NmeaInterpreter1.UtcDateTimeChanged, AddressOf NmeaInterpreter1_UtcDateTimeChanged
        AddHandler NmeaInterpreter1.Paused, AddressOf NmeaInterpreter1_Paused
        AddHandler NmeaInterpreter1.BearingChanged, AddressOf NmeaInterpreter1_BearingChanged
        AddHandler NmeaInterpreter1.VerticalDilutionOfPrecisionChanged, AddressOf NmeaInterpreter1_VerticalDilutionOfPrecisionChanged
        AddHandler NmeaInterpreter1.SatellitesChanged, AddressOf NmeaInterpreter1_SatellitesChanged
        'Timer
        Timer1.Enabled = True
        Timer1.Interval = 500
        AddHandler Timer1.Tick, AddressOf Timer1_Tick

        'Compass1.IsPaintingOnSeparateThread = False
        frmGPScompass.Compass1.IsPaintingOnSeparateThread = False
        'altimeter1.IsPaintingOnSeparateThread = False
        'speedometer1.IsPaintingOnSeparateThread = False
        ' SatelliteViewer1.IsPaintingOnSeparateThread = False
        frmGPScompass.SatelliteViewer1.IsPaintingOnSeparateThread = False
        'SatelliteSignalBar1.IsPaintingOnSeparateThread = False

        ' GeoFramework.Gps.Diagnostics.Run(New StreamWriter("GPS Diagnostics Log.txt"))

    End Sub
#Region "GPS Device : Detection des évènements"

    Private Sub Devices_DeviceDetectionStarted(ByVal sender As Object, ByVal e As EventArgs)
        BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionStarted), sender, e)
    End Sub
    Private Sub DeviceDetectionStarted(ByVal sender As Object, ByVal e As EventArgs)
        StatusBar2.Text = "Detection des appareils GPS ..."
        SearchButton.Enabled = False
        frmDataTracks.Txt_TrackStatus.Text = "false"
        GpsStatus = CtlLightGPSTrack.GpsLightStatus.statusOff
        ChangeCtlLightGpsStatus(GpsStatus)
    End Sub


    Private Sub Devices_DeviceDetected(ByVal sender As Object, ByVal e As DeviceEventArgs)
        BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetected), sender, e)
    End Sub
    Private Sub DeviceDetected(ByVal sender As Object, ByVal e As DeviceEventArgs)
        StatusBar2.Text = "Appareil trouvé: " & e.Device.ToString()
        Dim DeviceItem As New ListViewItem(e.Device.ToString())
        '  Associate the detected device with this list view item, so we can connect to the device later on
        DeviceItem.Tag = e.Device
        'Set the image of the new item
        DeviceItem.ImageIndex = 0
        DevicesListView.Items.Add(DeviceItem)
    End Sub

    Private Sub Devices_DeviceDetectionCompleted(ByVal sender As Object, ByVal e As EventArgs)
        BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionCompleted), sender, e)
    End Sub
    Private Sub DeviceDetectionCompleted(ByVal sender As Object, ByVal e As EventArgs)
        StatusBar2.Text = "detection completée"
        SearchButton.Enabled = True
        StartButton.Enabled = True
    End Sub

    Private Sub Devices_DeviceDetectionAttempted(ByVal sender As Object, ByVal e As DeviceEventArgs)
        BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionAttempted), sender, e)
    End Sub
    Private Sub DeviceDetectionAttempted(ByVal sender As Object, ByVal e As DeviceEventArgs)
        StatusBar2.Text = "Detection  " & e.Device.ToString()
    End Sub

    Private Sub Devices_DeviceDetectionAttemptFailed(ByVal sender As Object, ByVal e As DeviceDetectionExceptionEventArgs)
        BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionAttemptFailed), sender, e)
    End Sub
    Private Sub DeviceDetectionAttemptFailed(ByVal sender As Object, ByVal e As DeviceDetectionExceptionEventArgs)
        StatusBar2.Text = "Erreur:" & e.Exception.ToString()
    End Sub

    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        GeoFramework.Gps.IO.Devices.BeginDetection()

    End Sub

#End Region


#Region "interpreteur NMEA: Detection des évènements"
    Private Sub NmeaInterpreter1_AltitudeChanged(ByVal sender As Object, ByVal e As GeoFramework.DistanceEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.DistanceEventArgs)(AddressOf AltitudeChanged), sender, e)

    End Sub

    Private Sub AltitudeChanged(ByVal sender As Object, ByVal e As GeoFramework.DistanceEventArgs)
        ' altimeter1.Value = e.Distance
        strAltitude = e.Distance.ToLocalUnitType().ToString()
        Altitude.Text = strAltitude
        'strAltitude = Altitude.Text
        frmEntryGPS.strAltitude = strAltitude
        frmNouvSite.strAltitude = strAltitude
        ' ajouter données au track
        ' AfficherDataTrack(strLatitude, strLongitude, strHDOP, strAltitude)
    End Sub


    Private Sub NmeaInterpreter1_SpeedChanged(ByVal sender As System.Object, ByVal e As GeoFramework.SpeedEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.SpeedEventArgs)(AddressOf SpeedChanged), sender, e)

    End Sub

    Private Sub SpeedChanged(ByVal sender As Object, ByVal e As GeoFramework.SpeedEventArgs)

        'speedometer1.Value = e.Speed
        'Update the current speed
        Speed.Text = e.Speed.ToLocalUnitType().ToString()
        StatusBar2.Text = "Speed has changed."

    End Sub


    Private Sub NmeaInterpreter1_FixAcquired(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BeginInvoke(New Action(Of Object, System.EventArgs)(AddressOf FixAcquired), sender, e)

    End Sub

    Private Sub FixAcquired(ByVal sender As Object, ByVal e As System.EventArgs)

        'Change the fix status            
        FixStatus.Text = "Fix Obtained!"
        StatusBar2.Text = "A fix has been obtained!"

    End Sub


    Private Sub NmeaInterpreter1_FixLost(ByVal sender As System.Object, ByVal e As System.EventArgs)

        BeginInvoke(New Action(Of Object, System.EventArgs)(AddressOf FixLost), sender, e)

    End Sub

    Private Sub FixLost(ByVal sender As Object, ByVal e As System.EventArgs)

        'Change the fix status
        FixStatus.Text = "Fix Lost"
        StatusBar2.Text = "The current fix was lost."

    End Sub



    Private Sub NmeaInterpreter1_Resumed(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BeginInvoke(New Action(Of Object, System.EventArgs)(AddressOf Resumed), sender, e)

    End Sub

    Private Sub Resumed(ByVal sender As Object, ByVal e As System.EventArgs)

        Cursor.Current = Cursors.Default
        StatusBar2.Text = "Device is no longer suspended! Restarting..."

    End Sub



    Private Sub NmeaInterpreter1_FixModeChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.FixModeEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.Gps.FixModeEventArgs)(AddressOf FixModeChanged), sender, e)

    End Sub

    Private Sub FixModeChanged(ByVal sender As Object, ByVal e As GeoFramework.Gps.FixModeEventArgs)

        ' Update the current fix mode
        FixMode.Text = e.FixMode.ToString()
        StatusBar2.Text = "Fix mode has changed."

    End Sub


    Private Sub NmeaInterpreter1_FixQualityChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.FixQualityEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.Gps.FixQualityEventArgs)(AddressOf FixQualityChanged), sender, e)

    End Sub

    Private Sub FixQualityChanged(ByVal sender As Object, ByVal e As GeoFramework.Gps.FixQualityEventArgs)
        ' Update the current fix quality
        FixQuality.Text = e.FixQuality.ToString()
        StatusBar2.Text = "Fix quality has changed."
    End Sub


    Private Sub NmeaInterpreter1_HorizontalDilutionOfPrecisionChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.DilutionOfPrecisionEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.Gps.DilutionOfPrecisionEventArgs)(AddressOf HorizontalDilutionOfPrecisionChanged), sender, e)

    End Sub

    Private Sub HorizontalDilutionOfPrecisionChanged(ByVal sender As Object, ByVal e As GeoFramework.Gps.DilutionOfPrecisionEventArgs)
        'Update the horizontal DOP on the track bar
        'CurrentHdopTrackBar.Value = CInt(e.DilutionOfPrecision.Value)
        'UpdateDopStatus()
        StatusBar2.Text = "Lat/long precision has changed."
        'strHDOP = CurrentHdopTrackBar.Value
        frmEntryGPS.strDop = strHDOP
        frmNouvSite.strDop = strHDOP
        ' ajouter données au track
        'AfficherDataTrack(strLatitude, strLongitude, strHDOP, strAltitude)
    End Sub



    Private Sub NmeaInterpreter1_PositionChanged(ByVal sender As System.Object, ByVal e As GeoFramework.PositionEventArgs)

        BeginInvoke(New Action(Of Object, GeoFramework.PositionEventArgs)(AddressOf PositionChanged), sender, e)

    End Sub

    Private Sub PositionChanged(ByVal sender As Object, ByVal e As GeoFramework.PositionEventArgs)
        ' Update the latitude and longitude

        strLatitude = e.Position.Latitude.ToString()
        strLongitude = e.Position.Longitude.ToString()
        If strLatitude <> "NaN" And strLongitude <> "NaN" Then
            Latitude.Text = strLatitude
            Longitude.Text = strLongitude

            ' Latitude.Text = "00°00'0,00" & "N"
            ' Longitude.Text = "00°00'0,00" & "E"
            '  End If
            StatusBar2.Text = "Position has changed."

            frmEntryGPS.strLatitude = Latitude.Text
            frmEntryGPS.strLongitude = Longitude.Text
            frmNouvSite.strLatitude = Latitude.Text
            frmNouvSite.strLongitude = Longitude.Text
            frmPhotoGeoref.strLatitude = Latitude.Text
            frmPhotoGeoref.strLongitude = Longitude.Text
            ' strLongitude = Longitude.Text
            ' ajouter données au track
            ' If strLatitude <> Nothing And strLongitude <> Nothing Then
            AfficherDataTrack(strLatitude, strLongitude, strHDOP, strAltitude)
        End If
    End Sub



    Private Sub NmeaInterpreter1_UtcDateTimeChanged(ByVal sender As System.Object, ByVal e As GeoFramework.DateTimeEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.DateTimeEventArgs)(AddressOf UtcDateTimeChanged), sender, e)

    End Sub

    Private Sub UtcDateTimeChanged(ByVal sender As Object, ByVal e As GeoFramework.DateTimeEventArgs)
        ' Update the current satellite-derived time
        SatelliteTime.Text = e.DateTime.ToLocalTime().ToString()
    End Sub

    Private Sub NmeaInterpreter1_Paused(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BeginInvoke(New Action(Of Object, System.EventArgs)(AddressOf Paused), sender, e)

    End Sub

    Private Sub Paused(ByVal sender As Object, ByVal e As System.EventArgs)
        StatusBar2.Text = "Device was powered off!"
        Cursor.Current = Cursors.WaitCursor
    End Sub


    Private Sub NmeaInterpreter1_BearingChanged(ByVal sender As System.Object, ByVal e As GeoFramework.AzimuthEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.AzimuthEventArgs)(AddressOf BearingChanged), sender, e)

    End Sub

    Private Sub BearingChanged(ByVal sender As Object, ByVal e As GeoFramework.AzimuthEventArgs)
        'Compass1.Value = e.Azimuth
        frmGPScompass.Compass1.Value = e.Azimuth
        'Output the current bearing as degrees (i.e. 012°)
        Bearing.Text = e.Azimuth.ToString()
        'Output the current bearing as a compass direction (i.e. Southwest)
        Direction.Text = e.Azimuth.ToString("c")
        StatusBar2.Text = "Bearing has changed."

    End Sub

    Private Sub NmeaInterpreter1_VerticalDilutionOfPrecisionChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.DilutionOfPrecisionEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.Gps.DilutionOfPrecisionEventArgs)(AddressOf VerticalDilutionOfPrecisionChanged), sender, e)

    End Sub

    Private Sub VerticalDilutionOfPrecisionChanged(ByVal sender As Object, ByVal e As GeoFramework.Gps.DilutionOfPrecisionEventArgs)

        ' Update the vertical DOP on the track bar
        'CurrentVdopTrackBar.Value = CInt(e.DilutionOfPrecision.Value)
        'UpdateDopStatus()
        StatusBar2.Text = "Altitude precision has changed."

    End Sub


    Private Sub NmeaInterpreter1_SatellitesChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.SatelliteListEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.Gps.SatelliteListEventArgs)(AddressOf SatellitesChanged), sender, e)

    End Sub

    Private Sub SatellitesChanged(ByVal sender As Object, ByVal e As GeoFramework.Gps.SatelliteListEventArgs)
        'SatelliteViewer1.Satellites = TryCast(e.Satellites, List(Of Satellite))
        frmGPScompass.SatelliteViewer1.Satellites = TryCast(e.Satellites, List(Of Satellite))
        'SatelliteSignalBar1.Satellites = TryCast(e.Satellites, List(Of Satellite))
    End Sub


#End Region

#Region "Connection et Deconnection"
    Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
        If DevicesListView.SelectedIndices.Count = 0 Then

            MessageBox.Show("Merci de sélectionner un appareil pour commencer")

            Return

        End If

        ' Find out which item is selected in the list view

        Dim SelectedItem As ListViewItem = DevicesListView.Items(DevicesListView.SelectedIndices(0))


        ' The "tag" property holds the device that was detected

        Dim selectedDevice As Device = DirectCast(SelectedItem.Tag, Device)
        oCurrentDevice = selectedDevice
        

        Try
            NmeaInterpreter1.Start(selectedDevice)
            AddHandler selectedDevice.Connecting, AddressOf Device_Connecting
            AddHandler selectedDevice.Connected, AddressOf Device_Connected
            AddHandler selectedDevice.Disconnecting, AddressOf Device_Disconnecting
            AddHandler selectedDevice.Disconnected, AddressOf Device_Disconnected

            StartButton.Enabled = False

            ' Enable the disconnect button

            StopButton.Enabled = True

        Catch ex As Exception
            MessageBox.Show(ex.ToString(), "Connexion impossible", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        End Try

        ' Finally, start the interpreter using the newly-assigned Stream

    End Sub
    Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
        StopGPS()

    End Sub

    Public Sub StopGPS()
        ' arreter toute communication
        NmeaInterpreter1.Stop()
        m_isGpsInterpreteurOpen = False
        StartButton.Enabled = True
        ' désactiver le bouton deconnexion
        StopButton.Enabled = False
        StatusBar2.Text = "deconnexion du GPS"

        blGpsON = False
    End Sub


    Private Sub DevicesListView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DevicesListView.SelectedIndexChanged

        StartButton.Enabled = True
        StartButton.Focus()
    End Sub

    Sub Device_Connecting(ByVal sender As Object, ByVal e As EventArgs)

        Cursor.Current = Cursors.WaitCursor
        StatusBar2.Text = "Connection à l'appareil GPS..."
        GpsStatus = CtlLightGPSTrack.GpsLightStatus.statusOnWithoutSignal
        ChangeCtlLightGpsStatus(GpsStatus)
    End Sub



    Sub Device_Connected(ByVal sender As Object, ByVal e As EventArgs)

        StatusBar2.Text = "Connecté au GPS! lecture de données..."
        Cursor.Current = Cursors.[Default]
        GpsStatus = CtlLightGPSTrack.GpsLightStatus.statusOn
        ChangeCtlLightGpsStatus(GpsStatus)
    End Sub

    Sub Device_Disconnecting(ByVal sender As Object, ByVal e As System.EventArgs)

        Try

            StatusBar2.Text = "Disconnexion du GPS ..."

            Cursor.Current = Cursors.WaitCursor

        Catch
            MsgBox("GPS deconnecté")
        End Try

    End Sub

    Sub Device_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs)

        Try

            StatusBar2.Text = "Deconnecté."

            Cursor.Current = Cursors.[Default]
            GpsStatus = CtlLightGPSTrack.GpsLightStatus.statusOff
            ChangeCtlLightGpsStatus(GpsStatus)
        Catch

        End Try

    End Sub

#End Region
#Region "Working with Raw Data"

    Public Sub NmeaInterpreter1_SentenceReceived(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.Nmea.NmeaSentenceEventArgs)
        BeginInvoke(New Action(Of Object, GeoFramework.Gps.Nmea.NmeaSentenceEventArgs)(AddressOf SentenceReceived), sender, e)

    End Sub


    Private Sub SentenceReceived(ByVal sender As Object, ByVal e As GeoFramework.Gps.Nmea.NmeaSentenceEventArgs)


        'Keep the list box from getting too big
        If (RawDataListBox.Items.Count > 50) Then
            RawDataListBox.Items.RemoveAt(0)
        End If
        'Add the sentence to the raw data list box
        RawDataListBox.Items.Add(e.Sentence.ToString())
        RawDataListBox.SelectedIndex = RawDataListBox.Items.Count - 1
        m_isGpsInterpreteurOpen = True
        
        blGpsON = True
    End Sub

    'Private Sub UpdateDopStatus()
    '    'Are readings now precise enough to use?
    '    If (CurrentHdopTrackBar.Value <= MaximumHdopTrackBar.Value) Then
    '        HdopStatusLabel.Text = "Position readings are being accepted."
    '    Else : HdopStatusLabel.Text = "Position readings are being ignored."
    '    End If
    '    'Are readings now precise enough to use?
    '    If (CurrentVdopTrackBar.Value <= MaximumVdopTrackBar.Value) Then
    '        VdopStatusLabel.Text = "Altitude readings are being accepted."
    '    Else : VdopStatusLabel.Text = "Altitude readings are being ignored."
    '    End If


    'End Sub


#End Region

#Region "Dilution Of Precision"

    'Private Sub MaximumVdopTrackBar_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)

    '    If MaximumVdopTrackBar.Value > MaximumVdopTrackBar.Maximum Then

    '        MaximumVdopTrackBar.Value = MaximumVdopTrackBar.Maximum

    '    ElseIf MaximumVdopTrackBar.Value <= MaximumVdopTrackBar.Minimum Then

    '        MaximumVdopTrackBar.Value = MaximumVdopTrackBar.Minimum + 1

    '        If MaximumVdopTrackBar.Value = 0 Then

    '            MaximumVdopTrackBar.Value = 1

    '        End If

    '    End If

    '    ' Set the new maximum altitude precision error

    '    NmeaInterpreter1.MaximumVerticalDilutionOfPrecision = New DilutionOfPrecision(MaximumVdopTrackBar.Value)

    '    ' And update the UI

    '    UpdateDopStatus()

    'End Sub



    'Private Sub MaximumHdopTrackBar_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)

    '    If MaximumHdopTrackBar.Value > MaximumHdopTrackBar.Maximum Then

    '        MaximumHdopTrackBar.Value = MaximumHdopTrackBar.Maximum

    '    ElseIf MaximumHdopTrackBar.Value <= MaximumHdopTrackBar.Minimum Then

    '        MaximumHdopTrackBar.Value = MaximumHdopTrackBar.Minimum

    '        If MaximumHdopTrackBar.Value = 0 Then

    '            MaximumHdopTrackBar.Value = 1

    '        End If

    '    End If

    '    ' Set the new maximum latitude/longitude precision error

    '    NmeaInterpreter1.MaximumHorizontalDilutionOfPrecision = New DilutionOfPrecision(MaximumHdopTrackBar.Value)

    '    ' And update the UI

    '    UpdateDopStatus()

    'End Sub

#End Region


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        LocalTime.Text = DateTime.Now.ToString()
    End Sub


    Private Sub btnRetourMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetourMenu.Click
        Me.Hide()
        FormMain.Show()
    End Sub

    Private Sub btnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetour.Click
        Me.Hide()
        frmNavigationTools.Show()
    End Sub
    Private Sub AfficherDataTrack(ByVal latitude As String, ByVal longitude As String, ByVal hDop As String, ByVal altitude As String)
        Dim wTrack, stLatitude, stLongitude As String
        Dim nbTrame, longueurChaine As Integer
        nbTrame = 0

        stLatitude = ""
        stLongitude = ""
        If hDop <> "" And hDop <> "0" And hDop <> "Null" And latitude <> "" And longitude <> "" Then
            If (CInt(hDop) <= 50) Then
                Dim DateNom As String = Now.ToString("yyyy-MM-ddTHH:mm:ssZ")

                ' conversion des angles de DMS --> degré decimal

                stLatitude = conversionAngle(latitude)
                stLongitude = conversionAngle(longitude)
                stLatitude = Replace(stLatitude, ",", ".")
                stLongitude = Replace(stLongitude, ",", ".")
                ' enlever l'unité de la chaine (m)

                If altitude <> "" Then
                    longueurChaine = altitude.Length
                    altitude = Replace(altitude, ",", ".")
                    altitude = altitude.Substring(0, longueurChaine - 2)
                End If
                If frmDataTracks.Txt_TrackStatus.Text = "true" Then
                    wTrack = "<trkpt lat=" & """" & stLatitude & """" & "  lon=" & """" & stLongitude & """" & " > <ele>" & altitude & "</ele> <time>" & DateNom & "</time> </trkpt>"
                    nbTrame = nbTrame + 1

                    ' Sauvegarde automatique toute les 200 trames
                    If nbTrame > 200 Then
                        SaveTrack(FormMain.NSDossier & "TrackGPS_" & Now.ToString("yyMMdd") & ".gpx", FormMain.NSDossier)
                        frmDataTracks.lb_Track.Text = ""
                        nbTrame = 0
                    End If

                    frmDataTracks.lb_Track.Text = frmDataTracks.lb_Track.Text + Chr(10) + wTrack.ToString
                End If
            Else : frmDataTracks.lb_Track.Text = "null"
            End If
        End If
    End Sub
    

End Class
